package meterMonitor.biz;

import meterMonitor.mapper.MonitorDayMapper;
import meterMonitor.model.MonitorDay;
import meterMonitor.model.MonitorDetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class MonitorDayBiz {
    public static final Logger logger = LoggerFactory.getLogger(MonitorDayBiz.class);
    @Autowired
    private MonitorDayMapper monitorDayMapper;


    public List<MonitorDay> query(MonitorDay monitorDay) {
        return monitorDayMapper.query(monitorDay);
    }

    public List<MonitorDay> queryData(String fromTime, String toTime, String codes) {
        return monitorDayMapper.queryData(fromTime, toTime,  codes);
    }

    //根据设备类型查询日数据
    public List<MonitorDay> queryDataByType(String fromTime, String toTime, Integer areaId,int deviceTypeCode,String codes) {
        return monitorDayMapper.queryDataByType(fromTime, toTime, areaId,deviceTypeCode, codes);
    }

    //当areaId为空
    public List<MonitorDay> queryDataByTypeWithNoAreaId(String fromTime, String toTime, String areaId,int deviceTypeCode,String codes) {
        return monitorDayMapper.queryDataByTypeWithNoAreaId(fromTime, toTime, areaId,deviceTypeCode, codes);
    }

    public List<MonitorDay> queryDataByTypeWithNoAreaId1(String areaId,int deviceTypeCode,String codes) {
        return monitorDayMapper.queryDataByTypeWithNoAreaId1(areaId,deviceTypeCode, codes);
    }


    //当选择区域条件为空时，


    public List<MonitorDay> queryDetailData(String fromTime, String toTime, String codes) {

        List<MonitorDay> monitorDays = monitorDayMapper.queryData(fromTime, toTime, codes);
        for (MonitorDay m : monitorDays) {
            if (m.getStandard() < m.getIntervalQuantity()) {
                m.setIsOver("是");
            } else {
                m.setIsOver("否");
            }
            // 设置当前读数
            MonitorDetail monitorDetail = monitorDayMapper.queryCurrentQuantity(m.getMeterCode(), m.getDay());
            if (Objects.nonNull(monitorDetail)) {
                m.setCurrentQuantiry(monitorDetail.getCurrentQuantiry());
            }
        }

        return monitorDays;
    }

    public MonitorDay getCurrentByMeterCode(Long meterCode) {
        return monitorDayMapper.getCurrentByMeterCode(meterCode);
    }
}

